package com.imranstechnology.homeexpensemanager.utils;

import android.annotation.SuppressLint;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@SuppressLint("SimpleDateFormat")
public final class DateUtil {
	public static final String DATE_FORMAT = "dd/MM/yyyy";
	public static final String STORE_DATE_FORMAT = "yyyyMMdd";
	private static final SimpleDateFormat DF = new SimpleDateFormat(DATE_FORMAT);
	private static Calendar calendar = Calendar.getInstance();
	public static CharSequence dateToString(Date date){
		return DF.format(date);
	}
	public static CharSequence dateToString(Date date, String format){
		SimpleDateFormat df = new SimpleDateFormat(format);
		return df.format(date);
	}

	public static Date stringToDate(String date){
		if(date == null || date.length() == 0)
			return null;
		try {
			return DF.parse(date);
		} catch (ParseException e) {
			return null;
		}
	}
	public static Date stringToDate(String date, String format){
		SimpleDateFormat df = new SimpleDateFormat(format);
		if(date == null || date.length() == 0)
			return null;
		try {
			return df.parse(date);
		} catch (ParseException e) {
			return null;
		}
	}

	public static Date truncateDate(Date date){
		if(date == null)
			return null;
		calendar.clear();
		calendar.setTime(date);
		calendar.clear(Calendar.HOUR);
		calendar.clear(Calendar.MINUTE);
		calendar.clear(Calendar.SECOND);
		calendar.clear(Calendar.MILLISECOND);
		calendar.clear(Calendar.HOUR_OF_DAY);
		return calendar.getTime();
	}
	public static boolean isGT(Date a, Date b){
		if(a == null || b == null)
			return false;
		return a.compareTo(b) > 0;
	}
	public static boolean isEQ(Date a, Date b){
		if(a == null || b == null)
			return false;
		return a.compareTo(b) == 0;
	}
	public static boolean isLT(Date a, Date b){
		if(a == null || b == null)
			return false;
		return a.compareTo(b) < 0;
	}
	public static boolean isSameDay(Date a, Date b){
		Date a1 = truncateDate(a);
		Date b1 = truncateDate(b);
		return isEQ(truncateDate(a1), truncateDate(b1));
	}
	public static boolean isSameMonth(Date a, Date b){
		if(a == null || b == null)
			return false;
		int aYear =0;
		int aMonth = 0;
		int bYear = 0;
		int bMonth = 0;
		calendar.clear();
		calendar.setTime(a);
		aYear = calendar.get(Calendar.YEAR);
		aMonth = calendar.get(Calendar.MONTH);
		calendar.clear();
		calendar.setTime(b);
		bYear = calendar.get(Calendar.YEAR);
		bMonth = calendar.get(Calendar.MONTH);
		
		return (aYear == bYear && aMonth == bMonth);
	}
	public static boolean isSameYear(Date a, Date b){
		if(a == null || b == null)
			return false;
		int aYear =0;
		int bYear = 0;
		calendar.clear();
		calendar.setTime(a);
		aYear = calendar.get(Calendar.YEAR);
		calendar.clear();
		calendar.setTime(b);
		bYear = calendar.get(Calendar.YEAR);
		return (aYear == bYear);
	}
	public static Date getCurrentDate(){
		calendar.clear();
		calendar.setTimeInMillis(System.currentTimeMillis());
		return calendar.getTime();
	}
	public static int getCurrent(int field){
		calendar.clear();
		calendar.setTime(getCurrentDate());
		return calendar.get(field);
	}
	public static int getCurrentDay(){
		calendar.setTime(getCurrentDate());
		return calendar.get(Calendar.DAY_OF_MONTH);
	}
	public static int getCurrentMonth(){
		calendar.setTime(getCurrentDate());
		return calendar.get(Calendar.MONTH);
	}
	public static int getCurrentYear(){
		calendar.setTime(getCurrentDate());
		return calendar.get(Calendar.YEAR);
	}
	public static int getCurrentHour(){
		calendar.setTime(getCurrentDate());
		return calendar.get(Calendar.HOUR);
	}
	public static int getCurrentHourOfDay(){
		calendar.setTime(getCurrentDate());
		return calendar.get(Calendar.HOUR_OF_DAY);
	}
	public static int getCurrentMinute(){
		calendar.setTime(getCurrentDate());
		return calendar.get(Calendar.MINUTE);
	}
	public static int getCurrentSecond(){
		calendar.setTime(getCurrentDate());
		return calendar.get(Calendar.SECOND);
	}
	public static int getCurrentMillSecond(){
		calendar.setTime(getCurrentDate());
		return calendar.get(Calendar.MILLISECOND);
	}
	public static int getDateFiled(Date date, int field){
		calendar.clear();
		calendar.setTime(date);
		return calendar.get(field);
	}
}
